(function ($) {
	$.DHCFitBoard = $.Class.extend({
		init:function(scrollId1,scrollId2,block){
			var self = this;
			self.scrollId1 = scrollId1;
			var keyborad = document.body.querySelector('.fitBoard');
			if (keyborad) {
				return;
			}
			var keyborad = document.createElement('div');
			keyborad.setAttribute('class','mui-popover fitBoard');
			document.body.appendChild(keyborad);
			keyborad.innerHTML = 
				'<div class="rights">All Rights Reserved ©东华医为</div>'+
				'<div class="fitClose nurfont nur-error"></div>'+
				'<div class="itemArea">'+
					'<div class="oneItem"><div class="itemTip">符合:</div><div class="itemText"></div></div>'+
					'<div class="oneItem"><div class="itemTip">部分符合:</div><div class="itemText"></div></div>'+
					'<div class="oneItem"><div class="itemTip">不符合:</div><div class="itemText"></div></div>'+
					'<div class="oneItem"><div id="notFit" data-key="-" class="notFit">不适用</div></div>'+
				'</div>'+
				'<div class="numArea">'+
					'<button class="fitNumBtn">7</button><button class="fitNumBtn">8</button><button class="fitNumBtn">9</button>'+
					'<button class="fitNumBtn">4</button><button class="fitNumBtn">5</button><button class="fitNumBtn">6</button>'+
					'<button class="fitNumBtn">1</button><button class="fitNumBtn">2</button><button class="fitNumBtn">3</button>'+
					'<button class="fitNumBtn clear">清空</button><button class="fitNumBtn">0</button>'+
					'<button class="fitNumBtn fitmark">备注</button>'+
				'</div>'+
				'<div class="upDownArea">'+
					'<button class="fitBack nurfont pad-backspace"></button>'+
					'<button class="fitUp mui-icon mui-icon-pulldown"></button>'+
					'<button class="fitDown mui-icon mui-icon-pulldown"></button>'+
				'</div>';
			var itemTexts = keyborad.querySelectorAll('.itemText');
			for (var i= 0; i< itemTexts.length;i++) {
				var itemText = itemTexts[i];
				itemText.addEventListener('tap',function(){
					var itemArea = document.body.querySelector('.fitBoard .itemArea');
					var oldActive = itemArea.querySelector('.dhc-active');
					if (oldActive) {
						oldActive.classList.remove('dhc-active')
					}
					this.classList.add('dhc-active')
				});
			}
			//不适用
			var notFit = keyborad.querySelector('.notFit');
			notFit.addEventListener('tap',function(){
				var newScore = this.getAttribute('data-key');
				block('notFit',this.innerText,newScore);
			});	
			var numberBtns = keyborad.querySelectorAll('.fitNumBtn');
			for (var i = 0; i < numberBtns.length;i++) {
				var numberBtn = numberBtns[i];
				numberBtn.addEventListener('tap',function(){
					var classStr = this.getAttribute('class');
					if (classStr.indexOf('clear') != -1) {//清空
						var itemArea = document.querySelector('.fitBoard .itemArea');
						var itemTexts = itemArea.querySelectorAll('.itemText');	
						for (var i = 0; i < itemTexts.length;i++) {
							itemTexts[i].innerHTML = ''
						}
						block('clear','','');
						return
					}
					if (classStr.indexOf('fitmark') !=-1) {//备注
						block('remark','');
						return
					}
					var itemArea = document.querySelector('.fitBoard .itemArea');
					var activeItem = itemArea.querySelector('.dhc-active');
					var oldStr = activeItem.innerText;
					if (oldStr == '0') {
						activeItem.innerText = this.innerText;
					}else if(oldStr.length < 3){
						activeItem.innerText = oldStr + this.innerText;
					}else{
						mui.toast("您输入的数值有点大哦~~")
						return;
					}
					var dict = itemTextChange();
					block('add',dict.inputText,dict.newScore);
				});
			}
			var fitBack = keyborad.querySelector('.fitBack');
			fitBack.addEventListener('tap',function(){
				var itemArea = document.querySelector('.fitBoard .itemArea');
				var activeItem = itemArea.querySelector('.dhc-active');
				var oldStr = activeItem.innerText;
				if (oldStr == '') {
					return;
				}
				activeItem.innerText = oldStr.substring(0,oldStr.length - 1)
				itemTextChange();
				var dict = itemTextChange();
				block('del',dict.inputText,dict.newScore);
			});
			
			
			var fitUp = keyborad.querySelector('.fitUp');
			fitUp.addEventListener('tap',function(){
				block('last');
			});
			var fitDown = keyborad.querySelector('.fitDown');
			fitDown.addEventListener('tap',function(){
				block('next');
			});
			var fitClose = keyborad.querySelector('.fitClose');
			fitClose.addEventListener('tap',function(){
				self.hideDHCKeyboard();
				block('close');
			});
			var fitArr = ['符合','部分符合','不符合']
			function itemTextChange(){
				var itemArea = document.querySelector('.fitBoard .itemArea');
				var itemTexts = itemArea.querySelectorAll('.itemText');	
				var newScore = '',inputText = '',tempStr = '';
				for (var i = 0; i < itemTexts.length;i++) {
					if (i != 0) {
						newScore += '」'
						tempStr += '」'
					}
					var str = itemTexts[i].innerText;
					newScore += str
					if (str!="") {
						if (inputText != '') {
							inputText += ','
						}
						inputText += fitArr[i]+':'+str
					}
				}
				if (newScore == tempStr) {
					inputText = '';
					newScore = '';
				}
				return {'inputText':inputText, 'newScore':newScore}
			}
		},
		updateDHCKeyboardUI:function(currReal){
			var itemArea = document.body.querySelector('.fitBoard .itemArea');
			var oldActive = itemArea.querySelector('.dhc-active');
			var first = itemArea.querySelector('.itemText');
			if (oldActive) {
				oldActive.classList.remove('dhc-active')
			}
			first.classList.add('dhc-active')
			var itemTexts = itemArea.querySelectorAll('.itemText');
			var realArr = currReal.split('」')
			for (var i = 0; i < itemTexts.length;i++) {
				var oneReal = realArr[i];
				if (oneReal == undefined || currReal == '-') {
					oneReal = '';
				}
				itemTexts[i].innerText = oneReal;
			}
		},
		showDHCKeyboard:function(){
			var keyborad = document.body.querySelector('.fitBoard');
			keyborad.classList.add('mui-active');
			keyborad.style.display = 'block';
			var scroll1 = document.getElementById(this.scrollId1);
			scroll1.style.paddingBottom = 350*SCALE + 30 + 'px'; //多写一个cell 的行高 不抖动
		},
		hideDHCKeyboard:function(){
			var keyborad = document.body.querySelector('.fitBoard');
			keyborad.classList.remove('mui-active');
			keyborad.style.display = 'none';
			var scroll1 = document.getElementById(this.scrollId1);
			scroll1.style.paddingBottom = '30px';
		}
	});
})(mui)